/*
 * @Author: wangkg wangkegui@longfor.com
 * @Date: 2024-08-08 15:05:13
 * @Description:
 */
import { TOKEN_KEY } from '@/enums/cache-enum';
import { createSessionStorage, createLocalStorage } from '@/utils/cache';

interface BasicStore {
  [TOKEN_KEY]: string | number | null | undefined;
}
export type BasicKeys = keyof BasicStore;

const isLocal = true;

const localMemory = createLocalStorage();
const sessionMemory = createSessionStorage();

export function getToken(): string {
  return getAuthCache(TOKEN_KEY);
}

export function setToken(token: any) {
  return setAuthCache(TOKEN_KEY, token);
}

export function getAuthCache(key: any) {
  const cache = isLocal ? localMemory : sessionMemory;
  return cache.get(key);
}

export function setAuthCache(key: any, value: any) {
  const cache = isLocal ? localMemory : sessionMemory;
  return cache.set(key, value);
}

export function clearAuthCache() {
  const cache = isLocal ? localMemory : sessionMemory;
  return cache.clear();
}
